
#include "pqxx_common.hpp"

namespace topdbs
{
	PqxCursor::PqxCursor(transaction_base& trans,const string& sSQL,const string& cursor_name,bool isHold)
		: m_connection_ptr(nullptr)
		, m_sql(sSQL)
		, m_dataindex(0)
		, m_cur_dataindex(0)
		, m_resultset(nullptr)
		, m_trans(trans)
		, m_stmt(nullptr)
		, m_cursor(nullptr)
		, m_cursor_name(cursor_name)
		//, m_stmt (new pqxx::prepare::invocation(trans,cursor_name))
		//, m_cursor( new pqxx::stateless_cursor<pqxx::cursor_base::read_only, pqxx::cursor_base::owned>(trans, sSQL, cursor_name, isHold))
	{
	}

	PqxCursor::~PqxCursor()
	{
		if(m_resultset)
		{
			delete m_resultset;
			m_resultset = nullptr;
		}	
		if(m_cursor)
		{
			m_cursor->close();
			delete m_cursor;
			m_cursor = nullptr;
		}
		if(m_stmt)
		{
			delete m_stmt;
			m_stmt = nullptr;
		}	
	}

	PqxStream::PqxStream()
		: m_connection_ptr(nullptr)
	{
	}

	PqxStream::~PqxStream()
	{
	}

	PqxStatement::PqxStatement(pqxx::connection* connection_ptr,transaction_base& trans,const string& statement)
		: m_connection_ptr(connection_ptr)
		, m_dataindex(0)
		, m_prepare_name(statement)
		, m_resultset(nullptr)
		, m_stmt (new pqxx::prepare::invocation(trans,statement))
	{
	}

	PqxStatement::~PqxStatement()
	{
		if(m_resultset)
		{
			delete m_resultset;
			m_resultset = nullptr;
		}
		if(m_stmt)
		{
			delete m_stmt;
			m_stmt = nullptr;
		}
	}
}